


import { setPower, savePower } from '../services/API';
import { message } from 'antd';

export default {
    namespace: "access",
    state: {
        powerList: []
    },

    effects: {
        *fetch({ payload }, { put, call }) {
            const res = yield call(setPower, payload);
            if (res.code === 200) {
                yield put({
                    type: 'save',
                    payload: {
                        powerList: res.data
                    }
                })
            }

        },
        *savPower({ payload }, { put, call }) {
            const res = yield call(savePower, payload);
            if (res.code === 200) {
                message.success(res.msg || 'Operation succeeded');
                yield put({
                    type: 'fetch',
                });
            }
        },

    },

    reducers: {
        save(state, action) {
            return { ...state, ...action.payload };
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            history.listen(({ pathname }) => {
                if (pathname === '/access') {
                    // dispatch({
                    //     type: "access/fetch",
                    // });
                }
            });
        },
    },
}